Page({
  data: {
    search:''

  },
  onLoad() {},
  doneSearch(ev){
    var fundList = my.getStorageSync({key:'fundList'});
    console.log(JSON.stringify({"KEY":"set前",value: fundList}))


    const fundCode=ev.detail.value;
    if(fundCode.length!=6){
      my.alert({title:"代码有误，请重新输入"})
      return;
    }

    if(fundList.data && fundList.data.includes(fundCode)){
      my.alert({title:"代码已存在"})
      return;
    }
    var hurl='https://fundgz.1234567.com.cn/js/'+fundCode+'.js'
    console.log(hurl)
    var verifyFlag=false;
    var that=this;
    my.httpRequest({
      url: hurl,
      method: 'GET',
      dataType:'string',
      success: function(res) {
        console.log(JSON.stringify(res))
        // my.alert({content: JSON.stringify(res.data)});
      },
      // fail: function(res) {
      //   console.log(JSON.stringify(res))
      //   my.alert({content:'代码有误，请重新输入'});
      //   return
      // },
      complete: function(res) {
        console.log(JSON.stringify(res.status))
        if(res.status!=200){
          my.alert({content:'未找到代码对应的天气，请重新输入'});
          return;
        }
        console.log({"200":res.status==200})
        console.log({"!200":res.status!=200})
        if(res.status==200){
          verifyFlag=true;
        }




        
    if(verifyFlag){

      if( !fundList.data){
        fundList=[]
        fundList.push(fundCode)
        my.setStorage({
          key: 'fundList',
          data: fundList,
          success: (res) => {
            console.log({ title: "setStorage success" })
          },
          fail: (err) => {
            console.log({ title: "setStorage success", content: JSON.stringify(err) });
          },
        });
        console.log(JSON.stringify({"KEY":"set后",value: fundList}))
        this.clearSearch();
        that.toPage('/page/tabBar/weather/weather')
      }else {
     
        fundList.data.push(fundCode)
        my.setStorageSync({
          key:'fundList',
          data:fundList.data,
          success: (res) => {
            console.log({ title: "setStorage success" })
          },
          fail: (err) => {
            console.log({ title: "setStorage success", content: JSON.stringify(err) });
          },
        })
        that.toPage('/page/tabBar/weather/weather');
      }
  
      }


      }
    });
    console.log({"verifyFlag":verifyFlag}) 


    
   
  },

  handleSearch(ev){
    const {data} = my.getStorageSync({key:'fundList'});
    console.log(data)
  },
  clearSearch(){
    this.search=''
    this.toPage('/page/tabBar/weather/weather');
  },

  toPage(pageUri){
    console.log(pageUri)
    my.switchTab({
      url: pageUri,
      success: () => {
        // my.showToast({
        //   content: '成功',
        //   type: 'success',
        //   duration: 4000
        // });
      }
    }
  );
  }
});
